﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ZombieServer
{
    class Program
    {
        static TcpListener listener;
        static bool started;
        static int clients;
        static List<Player> players;
        static void Main(string[] args)
        {
            while (true)
            {
                string s = Console.ReadLine();
                switch (s)
                {
                    case "start":
                        StartServer();
                        break;
                    case "clear":
                        Console.Clear();
                        break;
                    case "settings":
                        Console.WriteLine("Going over the settings file.");
                        Console.WriteLine("To leave something as it is don't type in anything.");
                        Console.WriteLine("To reset a value to default type in \"%\"");
                        Console.WriteLine("IP Address (Current Value: " + Settings.Default.ipaddr + ")");
                        string input = Console.ReadLine();
                        if (input != "")
                        {
                            if (input == "%")
                                Settings.Default.ipaddr = "";
                            else
                                Settings.Default.ipaddr = input;
                        }
                    a:
                        Console.WriteLine("Port (Current Value: " + Settings.Default.port + ")");
                        input = Console.ReadLine();
                        if (input != "")
                        {
                            if (input == "%")
                                Settings.Default.port = 51515;
                            else
                            {
                                int val = 0;
                                if (int.TryParse(input, out val))
                                    Settings.Default.port = val;
                                else
                                {
                                    Console.WriteLine("Invalid input, please insert numbers only (no floating point allowed)");
                                    goto a;
                                }
                            }
                        }
                    again:
                        Console.WriteLine("Max Players (Current Value: " + Settings.Default.maxpl + ")");
                        input = Console.ReadLine();
                        if (input != "")
                        {
                            if (input == "%")
                                Settings.Default.maxpl = 4;
                            else
                            {
                                int val = 4;
                                if (int.TryParse(input, out val))
                                    Settings.Default.maxpl = val;
                                else
                                {
                                    Console.WriteLine("Invalid input, please insert numbers only (no floating point allowed)");
                                    goto again;
                                }
                            }
                        }
                    again2:
                        Console.WriteLine("Min Players (Current Value: " + Settings.Default.minpl + ")");
                        input = Console.ReadLine();
                        if (input != "")
                        {
                            if (input == "%")
                                Settings.Default.minpl = 4;
                            else
                            {
                                int val = 4;
                                if (int.TryParse(input, out val))
                                    Settings.Default.maxpl = val;
                                else
                                {
                                    Console.WriteLine("Invalid input, please insert numbers only (no floating point allowed)");
                                    goto again2;
                                }
                            }
                        }
                        //...
                        Settings.Default.Save();
                        break;
                }
            }
        }

        public static void StartServer()
        {
            if (started)
            {
                Console.WriteLine("The server has already started.");
                return;
            }
            IPAddress adr = IPAddress.Any;
            if (!string.IsNullOrEmpty(Settings.Default.ipaddr) && !IPAddress.TryParse(Settings.Default.ipaddr, out adr))
            {
                adr = IPAddress.Any;
                Console.WriteLine("The IP address specified in the settings is invalid.");
                return;
            }
            listener = new TcpListener(adr, Settings.Default.port);
            listener.Start();
            Console.WriteLine("Server is up!");
            Console.WriteLine("The server uses this address: " + listener.LocalEndpoint);
            Console.WriteLine("Waiting for connections...");
            if (clients <= Settings.Default.maxpl)
                HandleSocket(listener.AcceptSocket());
        }

        public static void HandleSocket(Socket s)
        {
            Console.WriteLine("A client connected using the address " + s.LocalEndPoint + ".");
        }
    }
}
